﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using CAGFIP.Edicao;

namespace XML
{
    class ImportXML
    {

        public bool ImportXml(string file)
        {

            Pergunta pergunta; 
            string enunciado = "dummy";
            string alt_correta = "dummy";
            string[] alt_incorreta = { "dummy", "dummy", "dummy", "dummy" };
            int id_assunto = 0;
            int i = 0;

            XmlTextReader reader = new XmlTextReader(file);
            while (reader.Read())
            {
                switch (reader.NodeType)
                {

                    case XmlNodeType.Element: // The node is an element.
                        switch (reader.Name)
                        {
                            case "Enunciado":
                                reader.Read();
                                enunciado = reader.Value;
                                break;
                                
                            case "Alt_correta":
                                reader.Read();
                                alt_correta  = reader.Value;
                                break;

                            case "Alt_incorreta1":
                                reader.Read();
                                alt_incorreta[i] = reader.Value;
                                i++;
                                break;

                            case "Alt_incorreta2":
                                reader.Read();
                                alt_incorreta[i] = reader.Value;
                                i++;
                                break;

                            case "Alt_incorreta3":
                                reader.Read();
                                alt_incorreta[i] = reader.Value;
                                i++;
                                break;

                            case "Alt_incorreta4":
                                reader.Read();
                                alt_incorreta[i] = reader.Value;
                                i = 0;    
                                break;

                            case "Id_assunto":
                                reader.Read();
                                id_assunto = Int32.Parse(reader.Value);
                                break;
                        }
                        break;
                        
                    case XmlNodeType.EndElement:
                        if (reader.Name.Equals("Pergunta"))
                        {
                            pergunta = new Pergunta(id_assunto, enunciado, alt_correta, alt_incorreta);
                            ControladorEdicao.PerguntaDAO.CadastrarPergunta(pergunta);
                        }
                        break;
                    
                }
            }

            return true;
        }
    }
}
